var template = `
<div class="crack">
    <div v-if="crackMsg.length > 0" class="msg">
        <div>-- 破解模块 --</div>
        <div v-for="key,index in crackMsg">[{{index+1}}] {{key}}</div>
        <div>破解耗时{{pastTime}}ms</div>
    </div>
    <div :class="'crack-'+!!queryHash" v-else>[破解结果] {{queryHash?'解密成功':'解密失败'}}</div>
    <hr/>
</div>
`;

let Crack = {
    name: 'Crack',
    components: {},
    template,
    data() {
        const {useSlots} = Vue;
        return {
            sharedState: store.state,
            controlSlot: !!useSlots().default,
            crackMsg: [],
            queryHash: '',
            parser: new DOMParser(),
            startTime: new Date().getTime(),
            pastTime: 0,
        }
    },
    methods: {
        async doCrack() {
            let inter=0;
            let count=0;
            console.log('||||||||||||||||||||||||||||||')
            inter=setInterval(()=>{
                count++;
                if(count>=10){
                    clearInterval(inter)
                    console.log('10秒内未破解成功')
                    this.logMsg('解密密码数据失败')
                }
                if(typeof(unsafeWindow)==='undefined'){
                    unsafeWindow=window
                }
                if(unsafeWindow.queryHash){
                    let queryHash = unsafeWindow.queryHash
                    this.saveHash(queryHash)
                    this.logMsg('|||||||||||||| 获取成功 ||||||||||')
                    this.logMsg('已解密密码数据'+queryHash)
                    this.logMsg('-- 破解成功 --')
                    clearInterval(inter)
                }else{
                    this.logMsg('|||||||||||||| 等待破解 ||||||||||'+count)
                }
            },1000)
            // 10秒后清除破解日志
            setTimeout(() => this.crackMsg = [], 10000)
        },
        logMsg(msg) {
            let newMsg = []
            if (Array.isArray(msg)) {
                msg.forEach(e => newMsg.push([new Date().fmt('[yyyy-MM-dd HH:mm:ss]'), e].join(' ')))
            } else {
                newMsg.push([new Date().fmt('[yyyy-MM-dd HH:mm:ss]'), msg].join(' '))
            }
            this.crackMsg.push(...newMsg)
            console.log(...newMsg)
            this.pastTime = new Date().getTime() - this.startTime
        },
        saveHash(queryHash) {
            this.queryHash = queryHash
            this.sharedState.queryHash = this.queryHash
        },
        getForText(src) {
            return new Promise((resolve, reject) => {
                if (!src) {
                    let msg = ['src 地址不能为空 ', src]
                    this.$message({message: msg, level: 'error'})
                    console.log(...msg)
                    reject(msg)
                    return
                }
                axios.get(src).then(res => {
                    if (res.data && res.status === 200) {
                        let html = res.data
                        resolve(html)
                    } else {
                        let msg = ['获取TEXT失败', src]
                        this.$message({message: msg, level: 'error'})
                        console.log(...msg, res)
                        resolve('')
                    }
                }).catch(err => {
                    let msg = ['获取TEXT失败 err', src]
                    this.$message({message: msg, level: 'error'})
                    console.log(...msg, err)
                    resolve('')
                })
            })
        },
        getForHomeHtml() {
            return new Promise((resolve, reject) => {
                axios.get('/').then(res => {
                    if (res.data && res.status === 200) {
                        let html = res.data
                        resolve(html)
                    } else {
                        let msg = ['获取首页HTML失败']
                        this.$message({message: msg, level: 'error'})
                        console.log(...msg, res)
                        resolve('')
                    }
                }).catch(err => {
                    let msg = ['获取首页HTML失败 err']
                    this.$message({message: msg, level: 'error'})
                    console.log(...msg, err)
                    resolve('')
                })
            })
        }
    },
    mounted: () => {
        const {proxy} = Vue.getCurrentInstance();
        console.log('%s mounted', proxy.$options.name);
        proxy.doCrack();
    },
};